const { ByteQueue } = require('./byteQueue');

class LedInterface {
    constructor() {
        // 控制模式 0:非控制模式 1:控制开机 2:控制关机 3:定时开关机
        this.InnerControl = 0;
        
        // 数据队列
        this.byteQueue = new ByteQueue(100);
        this.outputByteQueue = new ByteQueue(100);
        
        // 匹配标志
        this.FeiKongIsMatch = false;

        // 定义通信协议的各种标记和命令
        // 飞控开始标记+开关机主动发送的第一条指令
        this.FeiKongFlag = Buffer.from([0x46, 0x4B, 0x02, 0x00, 0x01, 0x0E, 0x4B, 0x46]);

        // 飞控响应开始标记+开关机响应的第一条指令
        this.FeiKongReplyFlag = Buffer.from([0x46, 0x4b, 0x04, 0x00, 0x01, 0x0e, 0x00, 0x00, 0x4b, 0x46]);

        // 开机命令
        this.FeiKongPowerOn = Buffer.from([0x46, 0x4b, 0x02, 0x00, 0x01, 0x11, 0x4b, 0x46]);

        // 开机命令响应
        this.FeiKongPowerOnReply = Buffer.from([0x46, 0x4b, 0x02, 0x00, 0x01, 0x11, 0x00, 0x00, 0x4b, 0x46]);

        // 关机命令
        this.FeiKongPowerOff = Buffer.from([0x46, 0x4b, 0x02, 0x00, 0x01, 0x12, 0x4b, 0x46]);

        // 关机命令响应
        this.FeiKongPowerOffReply = Buffer.from([0x46, 0x4b, 0x02, 0x00, 0x01, 0x12, 0x00, 0x00, 0x4b, 0x46]);

        // 第三条请求标记
        this.FeiKongThirdRequestFlag1 = Buffer.from([0x46, 0x4b, 0x02, 0x00, 0x01, 0x02, 0x4b, 0x46]);

        // 第三条请求响应
        this.FeiKongThirdResponseFlag = Buffer.from([0x46, 0x4b, 0x04, 0x00, 0x01, 0x21, 0x00, 0x00, 0x4b, 0x46]);

        // 文字帧开始标记
        this.FeiKongStartFlag = Buffer.from([0x5C, 0x6C, 0x61, 0x6E, 0x67]);

        // 文字帧结束标记
        this.FeiKongEndFlag = Buffer.from([0x5C, 0x70, 0x61, 0x72, 0x0D, 0x0A]);

        // 文字响应开头标记
        this.FeiKongContentStart = Buffer.from([0x46, 0x4b, 0xfc, 0x03, 0x01, 0x1b, 0x00, 0x10]);

        // 末尾帧数据标记
        this.FeiKongContentNext = Buffer.from([0x46, 0x4b]);

        // 定义响应模板
        this.FeiKongContentFirstReplayFlag = Buffer.from([0x46, 0x4B, 0x04, 0x00, 0x01, 0x0E, 0x00, 0x00, 0x4B, 0x46]);
    }

    // 从队列中获取单个数据包
    async getSingleFeiKongDataFromQueue() {
        const ret = [];
        const tmp = [];

        // 从队列中读取数据
        while (this.byteQueue.getSize() > 0) {
            const array = this.byteQueue.peek();
            
            // 检查是否是最后一个数据包
            if (this.matchBytes(array, this.FeiKongLastSend)) {
                tmp.push(this.byteQueue.take());
                break;
            } else {
                tmp.push(this.byteQueue.take());
            }
        }

        // 查找文本内容
        let startIndex = -1;
        let endIndex = -1;
        let startNum = 0;
        let endNum = 0;

        // 遍历所有数据包查找文本标记
        for (let i = 0; i < tmp.length; i++) {
            const content = tmp[i];
            
            // 查找开始标记
            if (startIndex === -1) {
                startIndex = this.findPattern(content, this.FeiKongStartFlag);
                if (startIndex !== -1) {
                    startNum = i;
                }
            }
            
            // 查找结束标记
            endIndex = this.findPattern(content, this.FeiKongEndFlag);
            if (endIndex !== -1) {
                endNum = i;
            }

            if (startIndex !== -1 && endIndex !== -1) {
                break;
            }
        }

        // 处理找到的文本内容
        if (startIndex !== -1 && endIndex !== -1) {
            // 提取文本内容并进行处理
            const content = this.extractContent(tmp, startIndex, endIndex, startNum, endNum);
            if (content) {
                // 检查是否需要过滤
                const isNeedRefuse = await this.isFeiKongMatchedContent(content);
                if (!isNeedRefuse) {
                    ret.push(...tmp);
                }
            }
        } else {
            // 没有找到文本内容，直接返回原数据
            ret.push(...tmp);
        }

        return ret;
    }

    // 辅助方法：字节匹配
    matchBytes(data, pattern) {
        if (!data || !pattern || data.length < pattern.length) {
            return false;
        }

        for (let i = 0; i < pattern.length; i++) {
            if (data[i] !== pattern[i]) {
                return false;
            }
        }
        return true;
    }

    // 辅助方法：查找模式
    findPattern(data, pattern) {
        for (let i = 0; i <= data.length - pattern.length; i++) {
            let found = true;
            for (let j = 0; j < pattern.length; j++) {
                if (data[i + j] !== pattern[j]) {
                    found = false;
                    break;
                }
            }
            if (found) {
                return i;
            }
        }
        return -1;
    }

    // 辅助方法：提取内容
    extractContent(packets, startIndex, endIndex, startNum, endNum) {
        // 如果开始和结束在同一个包中
        if (startNum === endNum) {
            const packet = packets[startNum];
            return packet.slice(startIndex + this.FeiKongStartFlag.length, endIndex);
        }

        // 如果跨越多个包
        let content = Buffer.alloc(0);
        for (let i = startNum; i <= endNum; i++) {
            const packet = packets[i];
            if (i === startNum) {
                // 第一个包
                content = Buffer.concat([content, packet.slice(startIndex + this.FeiKongStartFlag.length)]);
            } else if (i === endNum) {
                // 最后一个包
                content = Buffer.concat([content, packet.slice(0, endIndex)]);
            } else {
                // 中间的包
                content = Buffer.concat([content, packet]);
            }
        }
        return content;
    }

    // 构造响应数据
    getFeiKongDataContentReplay(index) {
        // 根据不同的index构造不同的响应
        switch(index) {
            case 0:
                // 第一个响应
                return this.FeiKongContentFirstReplayFlag;
            case 1:
                // 第二个响应
                return this.constructSecondReply();
            // ... 其他情况
        }
    }
}

module.exports = { LedInterface };
